import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== private component
const Page 		= $Lazy.load(()=>import('#tp/page/page-container-scroll'))
const Box		= $Lazy.load(()=>import('#tp/box/box'))
const message 	= $Lazy.file(()=>import('@antd/message'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
//
const AddDicModal = $Lazy.state(()=>import('#private/pages/add-dic-modal'))
// 
const ReferenceRangeTable 			= $Lazy.state(()=>import('../set-single-project/pages/c/table/reference-range'))
const SuggestionsExplanationsTable 	= $Lazy.state(()=>import('../set-single-project/pages/d/table/suggestions-explanations'))
const ReportResultDictionaryTable 	= $Lazy.state(()=>import('../set-single-project/pages/d/table/report-result-dictionary'))
const ResultConversionRuleTable 	= $Lazy.state(()=>import('../set-single-project/pages/d/table/result-conversion-rule'))
// ===================================================================== data
// ===================================================================== component
class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			{ label: '项目全称',			name: 'kind_name', 				half:true, required:true, title:'项目基本信息'},
			{ label: '项目简称',			name: 'short_name', 			half:true },
			{ label: '英文全称',			name: 'name_en', 				half:true },
			{ label: '英文简称',			name: 'name_short_en', 			half:true },
			{ label: '项目代码',			name: 'kind_code', 				width:'25%', disabled:true },
			{ label: '检测方法',			name: 'check_method',			width:'25%', 	type:'select', loading:true, idStr:'code', btn: { label:'添加', onClick:()=>{
			  	this.dicRef.open('检测方法', 'check_method', 16000)
			}}},
			{ label: '助记码',			name: 'debit_code', 			half:true },
			{ label: '备注',			name: 'memo', 					full:true, type:'textarea' },
			
			{ label: '上级质控',			name: 'superiorquality', 		half:true, title:'结果信息', suffix:'%', value:0},
			{ label: '结果类型',			name: 'result_type',  		width:'25%', required:true, type:'select', loading:true, idStr:'code', onValue:({value, formData})=>{
				$fn.hideRows(formData, 'result_dis_code', +value !== 1203)
				this.setState({ formData })
			} },
			{ label: '结果提示类型',		name: 'result_tips_temp',  		width:'25%', type:'select', loading:true, idStr:'uuid', nameStr:'temp_name' },
			{ label: '所属字典分类',		name: 'result_dis_code',  	half:true, type:'select', loading:true, hide:true },
			{ label: '结果默认值',		name: 'result_default_value',	width:'25%' },
			{ label: '结果计算公式',		name: 'result_formula', 		half:true },
			{ label: '结果单位',			name: 'result_unit',			width:'25%', 	type:'select', loading:true, idStr:'code' },
			{ label: '小数点位数',		name: 'decimal_point', 			half:true, type:'number' },
			
			{ label: '临床可报告范围',	names: ['clinical_range','clinical_range_end'], 		half:true, type:'range', br:true, value:0 },
			{ label: '线性范围',			names: ['linear_range','linear_range_end'], 			half:true, type:'range', br:true, value:0 },
		],
		offTips: {
			comb: [],
			market: [],
			single: [],
			sub: []
		}
	}
	
	componentDidMount(){
		const { kind_id } = $fn.query()
		if(kind_id){
			this.submit = { uuid: kind_id }
			this.initSubmit = { result_dis_code:'' }
			this.kind_id = kind_id
			this.setState({ kind_id })
		}
		this.getSelect()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			const dic = await f.getDicCache({ code: [ 16000, 18000, 1200] })
			// 检测方法
			$fn.setRow( formData, 'check_method', dic['16000'])
			// 结果单位
			$fn.setRow( formData, 'result_unit', dic['18000'])
			// 结果类型
			$fn.setRow( formData, 'result_type', dic['1200'])
			// 结果提示类型
			$fn.setRow( formData, 'result_tips_temp',  await f.getCache({ api: 'result_tips_type' }) )
			// 所属字典分类
			$fn.setRow( formData, 'result_dis_code', await f.getCache({ api:'dictionary/select' }))
			//
			this.setState({ formData })
		})
	}
	
	fetch = () => {
		$http.submit(null,'kd-subordinate-set/info', { param:{uuid:this.kind_id}, isPull:true } ).then((data)=>{
			const { formData } = this.state
			$fn.hideRows(formData, 'result_dis_code', +data.result_type !== 1203)
			this.formRef.setValue(data)
			this.setState({ edit: +data['kind_status'] === 2 })
		})
		$http.submit(null, 'kind-info/offTips', { param:{kind_id:this.kind_id} }).then(data => this.setState({offTips: data}))
	}
	
	// 操作按钮
	controls = () => {
		const param = this.kind_id
		const { edit } = this.state
		if( edit === true){
			const { comb, market, single, sub } = this.state.offTips
			return [
				Confirm.call(this, '下架', 'kind-info/off', { param, idStr:'kind_id', msg:
					<div>
					<span>{ single.length || sub.length || comb.length || market.length ? <span style={{color: 'red'}}>该项目下架，以下关联项目也将会被下架！</span> : '确认下架？' }</span><br />
						{ single.length ? <div>单一项目:【{single.join(',')}】<br /></div> : '' }
						{ sub.length ? <div>从属项目:【{sub.join(',')}】<br /></div> : '' }
						{ comb.length ? <div>组合项目:【{comb.join(',')}】<br /></div> : '' }
						{ market.length ? <div>营销项目:【{market.join(',')}】<br /></div> : '' }
					</div>,
					refresh:true, onComplete:()=>{
						this.setState({ edit: false })
				} }),
				Controls.back.call(this, { refresh:true })
			]
		}else if( edit === false){
			return [
				Confirm.call(this,'保存', null, { onMy: ()=> { 
					this.isAudit=false
					this.formRef.submit()
					$fn.dataRemove('specimen-kind/selectkind')
				} }),
				Confirm.call(this, '审批', 'kd-subordinate-set/audit', { param }),
				Confirm.call(this,'保存并提交审批', null, { onMy: ()=> { 
					this.isAudit=true
					this.formRef.submit()
					$fn.dataRemove('specimen-kind/selectkind')
				} }),
				Confirm.call(this, '发布', 'kd-subordinate-set/push', { param }),
				Controls.back.call(this, { refresh:true })
			]
		}else{
			return []
		}
	}
	
	render(){
		const tableStyle = {height:300, padding:'10px 0 10px'}
		const { kind_id } = this
		const { edit } = this.state
		return (
			<>
				<Page title={`${edit?'编辑':'新增'}从属项目`} nobc controls={this.controls()}>
					<Box contentClassName='plr10' first>
						{ 
							SubmitForm.call(this,{ msg:'保存', labelWidth:'s8', api:'kd-subordinate-set/edit', onForm: this.fetch, disabled:edit, onOk:()=>{
								if(this.isAudit){
									$http.submit(this,'kd-subordinate-set/audit', { param: { uuid:this.kind_id } }).then(data=>{
										message.then(f=>f.success('审批成功'))
									})
								}
							}}) 
						}
					</Box>
					<div className='fx mt5'>
						{/* 报告结果字典 */}
						<ReportResultDictionaryTable tableStyle={tableStyle} className='ex mr5' kind_id={kind_id} disabled={edit} />
						{/* 建议与解释 */}
						<SuggestionsExplanationsTable tableStyle={tableStyle} className='ex' kind_id={kind_id} disabled={edit}/>
					</div>
					{/* 结果转换规则 */}
					<ResultConversionRuleTable tableStyle={tableStyle} kind_id={kind_id} disabled={edit}/>
					{/* 参考范围 */}
					<ReferenceRangeTable tableStyle={tableStyle} kind_id={kind_id} disabled={edit}/>
				</Page>
				{/* 新增字典 */}
				<AddDicModal onRef={ref => this.dicRef = ref} top={this} />
			</>
		)
	}
}

export default Index